package com.liusy.serachengine;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.log4j.Logger;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexableField;


/**
 * 
 * 文件索引转换模型抽像类，包括搜索任务
 * 
 * @author liusy 2013-03-25
 *
 */
public class FileConvertModel extends AbstractConvertModel {

	public FileConvertModel(String createFileIndexPath)
	{
		file = new File(createFileIndexPath);
	}


	private File file = null;
	
	  
	private static Logger log = Logger.getLogger(FileConvertModel.class);
	  
	  
	public FileConvertModel(File createFileIndexFile)
	{
		file = createFileIndexFile;
	}
	
	
	/**
	 * 文件类型暂时不处理（doc,zip等）
	 * 
	 * @param file
	 * @return
	 */
	@Override
	public Document ObjectConvertDoc(Object obj) {
			File file = (File) obj; 
			Document doc = new Document();
			FileInputStream fis=null;
			try {
				fis = new FileInputStream(file);
				//new BufferedReader( new InputStreamReader(fis))
				//doc.add(new Field("ID", "1", Field.Store.YES, Field.Index.NOT_ANALYZED));
				doc.add(new StringField("path", file.getPath(), Field.Store.YES));
				doc.add(new LongField("modified", file.lastModified(), Field.Store.NO));
				doc.add(new TextField("content", file.getName(), Field.Store.NO));
				//doc.add(new TextField("content",new BufferedReader(new InputStreamReader(fis, "UTF-8"))));
			} catch (FileNotFoundException fnfe) {
				log.info(fnfe.getMessage());
				log.info("在创建文件索引时出错，该文件被忽略，未创建！");
				return null;
			}catch (Exception e) {
				e.printStackTrace();
				return null;
			}  finally {
				
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			return doc;
	}


	@Override
	public Object getCreateIndexTask() {
		
		return file;
	}
	
	

}
